﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Control;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;

namespace LPV_POS.mh_pos
{
    public partial class panel : DevExpress.XtraEditors.XtraForm
    {
        xulyngaythangnam ngaythang;
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        // select danh sach don hang hoa

        private DataTable f_selectdshanghoa()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // select danh sach nhom hang hoa
        private DataTable f_selectdsnhomhhforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 2 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn nhóm hàng ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        // select danh sach VAT
        private DataTable f_selectdsVATchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 3 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn thuế VAT ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // select danh sach don vi tinh
        private DataTable f_selectdsDVTchuforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 4 };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn ĐVT ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }
        // hien thi ds nhom chu len combobox
        private void f_showdataoncombox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }
        // insert hang hoa

        private int f_inserthanghoa(string tenhanghoa, string manhomhang, DateTime handung, Boolean banle, string mavat, string madvt, string mansx, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMHH ", "@HANDUNG", "@BANLE", "@VAT", "@MADVT", "@MANSX", "@GHICHU" };
            o = new object[] { 2, 1, tenhanghoa, manhomhang, handung, banle, mavat, madvt, mansx, ghichu };
            int ketqua = tv.SP_Update("sp_mh_dmhanghoa", s, o);
            return ketqua;
        }

        // update mot hang hoa
        private int f_updatehanghoa(string mahanghoa, string tenhanghoa, string manhomhang, DateTime handung, Boolean banle, string mavat, string madvt, string mansx, string ghichu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@MANHOMHH ", "@HANDUNG", "@BANLE", "@VAT", "@MADVT", "@MANSX", "@GHICHU" };
            o = new object[] { 3, 1, mahanghoa, tenhanghoa, manhomhang, handung, banle, mavat, madvt, mansx, ghichu };
            int ketqua = tv.SP_Update("sp_mh_dmhanghoa", s, o);
            return ketqua;
        }
        // xoa hang hoa
        private int f_deletehanghoa(string mahanghao)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, mahanghao };
            int ketqua = tv.SP_Update("sp_mh_dmhanghoa", s, o);
            return ketqua;
        }

        // Tim hang hoa khi nhan nut tim
        private DataTable f_selectfindhanghoa(string tenhanghoa, string manhomhang,string handung, string vat, string madvt, Boolean checkhandung)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@MANHOMHH", "@NGAYHANDUNG", "@VAT", "@MADVT", "@CHECK_HANDUNG" };
            o = new object[] { 1, 5, tenhanghoa, manhomhang, handung, vat, madvt, checkhandung };
            tb = tv.SP_SelectDataTable("sp_mh_dmhanghoa", s, o);
            return tb;
        }

        // tim hang hoa
        private DataTable f_findhh()
        {
            DataTable tb;

            string tenhanghoa;
            string manhomhang;
        //    DateTime handung;

            string thueVAT;
            string DVT;
            Boolean checkhandung;

            if (com_nhomhang.Text != "--- Chọn nhóm hàng ---")
                manhomhang = com_nhomhang._GetselectedID();
            else
                manhomhang = "";

            if (txt_tenhanghoa.Text != "")
                tenhanghoa = "'%" + txt_tenhanghoa.Text + "%'";
            else
                tenhanghoa = "";

            if (com_DVT.Text != "--- Chọn ĐVT ---")
                DVT = com_DVT._GetselectedID();
            else
                DVT = "";

            if (com_thueVAT.Text != "--- Chọn thuế VAT ---")
                thueVAT = com_thueVAT._GetselectedID();
            else
                thueVAT = "";



            if (check_handung.Checked == true)
                checkhandung = true;
            else
                checkhandung = false;
           // handung = Convert.ToDateTime(date_handung.Text);
            ngaythang = new xulyngaythangnam();
            string ngayhandung = ngaythang.chuyenngay(date_handung.Text);

            tb = f_selectfindhanghoa(tenhanghoa, manhomhang, ngayhandung, thueVAT, DVT, checkhandung);
            return tb;

        }

        public panel()
        {
            InitializeComponent();
            f_showdataoncombox(com_nhomhang, f_selectdsnhomhhforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_thueVAT, f_selectdsVATchuforcom(), "OBJID", "OBJNAME");
            f_showdataoncombox(com_DVT, f_selectdsDVTchuforcom(), "OBJID", "OBJNAME");
        }

        private void mh_dmhanghoa_Load(object sender, EventArgs e)
        {
            grid_hanghoa.OptionsBehavior.Editable = false;
            col_banle.FieldName = "BANLE";
            luoi_hanghoa.MainView = grid_hanghoa;
            luoi_hanghoa.DataSource = f_selectdshanghoa();
            bton_tim.Text = "CLR";
            check_handung.Visible = false;
            //  com_nhomchu._SetselectedID("1");
            //com_thueVAT._SetselectedID("1");
            bton_luu.Enabled = false;
            //bton_giatrithuVAT.Enabled = false;
            //bton_tenthueVAT.Enabled = false;
        }

        private void grid_hanghoa_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_hanghoa.FocusedColumn;
            com_nhomhang.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tennhomhang) + "";
            txt_tenhanghoa.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tenhanghoa) + "";
            com_DVT.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_DVT) + "";
            com_thueVAT.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tenthueVAT) + "";
            date_handung.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_handung) + "";
            string banle = grid_hanghoa.GetRowCellDisplayText(chiso, col_banle) + "";
            if (banle == "Checked")
                check_banle.Checked = true;
            else if (banle == "Unchecked")
                check_banle.Checked = false;

            bton_tenhanghoa.Enabled = false;
            bton_luu.Enabled = false;
            check_banle.Visible = true;
            lbel_banle.Visible = true;
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tenhanghoa.Text = "";
            com_nhomhang._SetselectedID("1");
            com_thueVAT._SetselectedID("1");
            com_DVT._SetselectedID("1");
            date_handung.Text = DateTime.Today.ToString("dd/MM/yyyy");
            bton_tenhanghoa.Enabled = true;
            bton_luu.Enabled = true;
            Variable.str_trangthaithemsua = "them";
            check_banle.Visible = true;
            lbel_banle.Visible = true;
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_tenhanghoa.Enabled = true;
            bton_luu.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
            check_banle.Visible = true;
            lbel_banle.Visible = true;
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_hanghoa.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string mahanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletehanghoa(mahanghoa);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_hanghoa.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_hanghoa.Views[0].DataSource;
                tam.Delete(chiso);
                check_banle.Visible = true;
                lbel_banle.Visible = true;
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {

            if (txt_tenhanghoa.Text == "" || com_nhomhang.Text == "--- Chọn nhóm hàng ---" || com_DVT.Text == "--- Chọn ĐVT ---" || com_thueVAT.Text == "--- Chọn thuế VAT ---")
                return;

            string tenhanghoa = txt_tenhanghoa.Text;
            string manhomhang = com_nhomhang._GetselectedID();
            DateTime handung = Convert.ToDateTime(date_handung.Text);
            Boolean banle;
            if (check_banle.Checked == true)
                banle = true;
            else
                banle = false;
            string mavat = com_thueVAT._GetselectedID();
            string madvt = com_DVT._GetselectedID();
            string mansx = "";
            string ghichu = "";
            string thueVAT;
            if (com_thueVAT.Text == "--- Chọn thuế VAT---")
                thueVAT = "";
            else
                thueVAT = com_thueVAT._GetselectedID();

            int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {

                ketqua = f_inserthanghoa(tenhanghoa, manhomhang, handung, banle, mavat, madvt, mansx, ghichu);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string mahanghoa = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa).ToString();
                ketqua = f_updatehanghoa(mahanghoa, tenhanghoa, manhomhang, handung, banle, mavat, madvt, mansx, ghichu);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // luoi_donvitinh.DataSource = f_selectdonvitinh();
            }
            luoi_hanghoa.DataSource = null;
            luoi_hanghoa.DataSource = f_selectdshanghoa();
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            if (bton_tim.Text == "CLR")
            {
                com_nhomhang.Text = "--- Chọn nhóm hàng ---";
                com_thueVAT.Text = "--- Chọn thuế VAT ---";
                com_DVT.Text = "--- Chọn ĐVT ---";
                txt_tenhanghoa.Text = "";
                date_handung.Text = DateTime.Today.ToString("dd/MM/yyyy");
                check_banle.Visible  = false;
                lbel_banle.Visible = false;
                bton_tim.Text = "Tìm";
                check_handung.Visible = true;
                check_handung.Checked = false;
            }
                  else
            {
                luoi_hanghoa.DataSource = null;
                luoi_hanghoa.DataSource = f_findhh();
                bton_tim.Text = "CLR";
                check_handung.Visible = false;
            }
            }

        private void bton_tenhanghoa_Click(object sender, EventArgs e)
        {

        }
        
    }
}